package com.hava.demo.springmvc.onlyone.controller.rest;

import com.hava.demo.springmvc.onlyone.entity.OnlyOne;
import com.hava.demo.springmvc.onlyone.service.OnlyOneService;
import com.hava.web.MediaType;
import com.hava.web.rest.Entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.List;

/**
 * Created by zhanpeng on 15-4-10.
 * //GET /entity : get entity all list
 //GET /entity/{id} : get entity.id={id} object
 //POST /entity : create new entity object
 //PUT /entity/{id} : update entity.id={id}
 //DELETE /entity/{id} : delete entity.id={id}*
 */
@RestController
@RequestMapping(value = "/api/demo/rest/v1/onlyone")
public class OnlyOneRestController {

    @Autowired
    private OnlyOneService onlyOneService;
    
    @Autowired
    //private Validation validation;
    
    //GET /entity : get entity all list
    //注意：这里与springside不同的地方是选择使用google common MediaType
    @RequestMapping(method = RequestMethod.GET,produces = MediaType.JSON)
    public List<OnlyOne> list(){
        return onlyOneService.getAllOnlyOne();
    }
    
    //GET /entity/{id} : get entity.id={id} object
    @RequestMapping(method = RequestMethod.GET,value = "/{id}",produces = MediaType.JSON)
    public OnlyOne get(@PathVariable("id") Long id){
        
        OnlyOne onlyOne = onlyOneService.getOnlyOne(id);
        
        if(onlyOne == null)
        {
            String message = "OnlyOne Object :id {" + id + "} not existed." ;
            System.out.println(message);
        }

        return onlyOne;
    }
    
    //POST /entity : create new entity object
    @RequestMapping(method = RequestMethod.POST,consumes = MediaType.JSON)
    public ResponseEntity<?> create(@RequestBody OnlyOne onlyOne,UriComponentsBuilder uriBuilder){
        
        System.out.println("create");
        //System.out.println("input:" + test);
        //TODO:JSR303校验

        onlyOneService.createOnlyOne(onlyOne);

        //按照Restful风格，返回新对象的
        HttpHeaders httpheader = new HttpHeaders();
        String entity_url = null;

        //通过反射获取entity的url
        String EntityURL = Entity.URL(this) + "/" + onlyOne.getId();

        System.out.println("EntityURL" + EntityURL);
        
        //生成Respone header显示Entity创建后的资源路径
        URI uri = uriBuilder.path(EntityURL).build().toUri();
        httpheader.setLocation(uri);
        ResponseEntity responseEntity = new ResponseEntity(httpheader, HttpStatus.CREATED);
        
        return responseEntity;
    }
    
    //PUT /entity/{id} : update entity.id={id}
    @RequestMapping(method = RequestMethod.PUT,value = "/{id}",consumes = MediaType.JSON)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@RequestBody OnlyOne onlyOne,@PathVariable("id") Long id)
    {
        onlyOne.setId(id);
        onlyOneService.updateOnlyOne(onlyOne);
    }

    //DELETE /entity/{id} : delete entity.id={id}
    @RequestMapping(method = RequestMethod.DELETE,value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable("id") Long id){
        onlyOneService.deleteOnlyOne(id);
    }
}
